﻿using _1.SchoolExercise.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Linq;
using System.Collections.Generic;

namespace SchoolExercise.Test
{    
    [TestClass()]
    public class StudentFactoryTest
    {
        [TestMethod()]
        public void CreateStudentTest()
        {
            string name = "Petar";
            string university = University.SofiaUni;
            
            Student actual = StudentFactory.CreateStudent(name, university);
            Assert.IsNotNull(actual, "Student was not created.");
        }

        [TestMethod()]
        public void CreateTwoStudentsTest()
        {
            string name1 = "Petar";
            string name2 = "Gosho";
            string university = University.SofiaUni;                        

            Student student1 = StudentFactory.CreateStudent(name1, university);
            Student student2 = StudentFactory.CreateStudent(name2, university);
            Assert.AreNotEqual(student1.FacultyNumber, student2.FacultyNumber, "Students have the same faculty numbers.");
        }

        [TestMethod()]
        public void AddStudentToCourseTest()
        {
            string name = "Petar";            
            string university = University.SofiaUni;

            Student pesho = StudentFactory.CreateStudent(name, university);
            Course KPK = new Course();
            KPK.AddStudent(pesho);

            Assert.IsTrue(KPK.Students.Contains(pesho), "Student was not inserted into course.");
        }

        [TestMethod()]
        public void AddManyStudentsToCourseTest()
        {
            Course KPK = new Course();

            for (int i = 0; i < Course.MaxStudentsInCourseCount; i++)
            {
                string name = "Petar";
                string university = University.SofiaUni;
                Student student = StudentFactory.CreateStudent(name, university);
                KPK.AddStudent(student);
            }                                    

            Assert.AreEqual(KPK.Students.Count() ,Course.MaxStudentsInCourseCount, "Not all students were added.");
        }

        [TestMethod()]
        [ExpectedException(typeof(Exception), "An extra student was added to the course.")]
        public void AddTooManyStudentsToCourseTest()
        {
            Course KPK = new Course();

            for (int i = 0; i < Course.MaxStudentsInCourseCount + 1; i++)
            {
                string name = "Petar";
                string university = University.SofiaUni;
                Student student = StudentFactory.CreateStudent(name, university);
                KPK.AddStudent(student);
            }            
        }

        [TestMethod()]        
        public void UniqueFNTest()
        {
            HashSet<int> fns = new HashSet<int>();

            for (int i = 0; i < 5000; i++)
            {
                string name = "Petar";
                string university = University.SofiaUni;
                Student student = StudentFactory.CreateStudent(name, university);

                Assert.IsTrue(!fns.Contains(student.FacultyNumber), "A student with the same FN already exists.");

                fns.Add(student.FacultyNumber);                
            }
        }
    }
}
